package controllers

import (
	"xxp_api/models"

	"github.com/astaxie/beego"
)

// 添加商品接口
type GoodsController struct {
	beego.Controller
}

// @Summary  获取所有商品列表
// @Description 商品列表
// @Param	uid 		query 	int	true	 "用户id"
// @Param	types 		query 	int	true	 "分类 1：热销，2：兑换券，3：线上商品，4：线下课程"
// @Param	sort 	    query 	int	true	 "排序规则  1：更新时间，2：返利，3：商品价格"
// @Param	order 	    query 	int	true	 "顺序  1：逆序，1正序"
// @Param	keystart 	query 	int	true	 "分页参数"
// @Success 200 {object} models.Response
// @router /list [post]
func (u *GoodsController) List() {
	var ret models.Response

	uid, _ := u.GetInt("uid")
	types, _ := u.GetInt("types")
	sort, _ := u.GetInt("sort")
	order, _ := u.GetInt("order")
	keystart, _ := u.GetInt("keystart")
	classify := u.GetString("classify")
	beego.Debug(u.Ctx.Request.Form)
	if uid <= 0 || types <= 0 || sort <= 0 || order <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {

		ret = models.GetGoodsList(uid, types, sort, order, keystart, classify)
	}
	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary  单个商品详情接口
// @Description 商品详情
// @Param	uid 		query 	int	true	 "用户id"
// @Param	gid 		query 	int	true	 "商品id"
// @Success 200 {object} models.Response
// @router /detail [post]
func (u *GoodsController) Detail() {
	var ret models.Response

	uid, _ := u.GetInt("uid")
	gid, _ := u.GetInt("gid")

	if uid <= 0 || gid <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.GetGoodsDetail(uid, gid)
	}
	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary  添加商品到店铺
// @Description 添加商品到店铺
// @Param	uid 		query 	int	true	 "用户id"
// @Param	gid 		query 	int	true	 "商品id"
// @Success 200 {object} models.Response
// @router /add [post]
func (u *GoodsController) Add() {
	var ret models.Response

	uid, _ := u.GetInt("uid")
	gid, _ := u.GetInt("gid")

	if uid <= 0 || gid <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.GetGoodsAdd(uid, gid)
	}
	u.Data["json"] = ret
	u.ServeJSON()
}

// @Summary  从店铺移除商品
// @Description 删除商品到店铺
// @Param	uid 		query 	int	true	 "用户id"
// @Param	gid 		query 	int	true	 "商品id"
// @Success 200 {object} models.Response
// @router /del [post]
func (u *GoodsController) Del() {
	var ret models.Response

	uid, _ := u.GetInt("uid")
	gid, _ := u.GetInt("gid")

	if uid <= 0 || gid <= 0 {
		ret.Code = models.ERRCODE_PARAMS_ERROR
		ret.Msg = "参数错误"
	} else {
		ret = models.GetGoodsDel(uid, gid)
	}
	u.Data["json"] = ret
	u.ServeJSON()
}

// @router /classify [post]
func (u *GoodsController) Classify() {
	u.Data["json"] = models.GetGoodsClassify()
	u.ServeJSON()
}
